import {MyHttp} from '../../services/myHttp/myhttp.service';
import { Injectable}  from '@angular/core';
import { Major} from '../../services/model/major';
import { User} from '../../services/model/user';
import { UserType} from '../../services/model/userType';
@Injectable()
export class UserService{
  constructor(private http:MyHttp){

  }
  getUserTypes():Promise<UserType[]>{
    return this.http.get({
      api:this.http.api.userTypes
    }).toPromise()
    .then((res)=>{
      let userTypes=[];
      if (res.status === 200) {
        let result=res.json();
        let seq=result.data;
        for(let ut of seq){
          let userType=new UserType();
          userType.userType=ut.userType;
          userType.userTypeDescription=ut.userTypeDescription;
          userTypes.push(userType);
        }
      }
      return Promise.resolve(userTypes);
    })
  }

  query(body:any):Promise<{count:number,items:User[]}>{
    return this.http.post({
      api:this.http.api.queryUser,
      body:body
    }).toPromise()
      .then((res)=> {
        if (res.status === 200) {
            let result=res.json();
          console.log(result);
            if(result.status){
              let items=result.data.items;
              let count=result.data.count;
              let users=[];
              for(let o in items){
                let item=new User();
                item.userId=items[o].userId;
                item.username=items[o].username;
                item.password=items[o].password;
                item.userType=items[o].userType;
                item.userTypeDescription=items[o].userTypeDescription;
                item.name=items[o].name;
                users.push(item);
              }
              console.log(users);
              return Promise.resolve({count:count,items:users});
            }
        }

      });
  }
  add(body:any):Promise<{status:boolean,message:string}>{
    return this.http.post({
      api:this.http.api.addUser,
      body:body
    }).toPromise()
      .then((res)=>{
        let data={
          status:false,
          message:'添加失败'
        };
        if (res.status === 200) {
          let result=res.json();
          data.status=result.status;
          data.message=result.message;
        }
        return Promise.resolve(data);
      })
  }
  update(body:any):Promise<{status:boolean,message:string}>{
    return this.http.post({
      api:this.http.api.updateUser,
      body:body
    }).toPromise()
      .then((res)=>{
        let data={
          status:false,
          message:'修改失败！'
        };
        if (res.status === 200) {
          let result=res.json();
          data.status=result.status;
          data.message=result.message;
        }
        return Promise.resolve(data);
      })
  }
  remove(id:number):Promise<{status:boolean,message:string}>{
    return this.http.post({
      api:this.http.api.removeUser,
      body:{id:id}
    }).toPromise()
      .then((res)=>{
        let data={
          status:false,
          message:'删除失败！'
        };
        if (res.status === 200) {
          let result=res.json();
          data.status=result.status;
          data.message=result.message;
        }
        return Promise.resolve(data);
      })
  }
}

